package com.cn.ant.modules.school.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.modules.school.dao.SchoolMajorDao;
import com.cn.ant.modules.school.entity.School;
import com.cn.ant.modules.school.entity.SchoolMajor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 学校专业表Service
 * @author AntDream
 * @version 2015-08-26
 */
@Service
@Transactional(readOnly = true)
public class SchoolMajorService extends BaseService {

    @Autowired
	private SchoolMajorDao schoolMajorDao;

	public SchoolMajor get(String id) {
		return schoolMajorDao.get(id);
	}
	
	public List<SchoolMajor> findList(SchoolMajor schoolMajor) {
		return schoolMajorDao.findList(schoolMajor);
	}
	
	public Page<SchoolMajor> findPage(Page<SchoolMajor> page, SchoolMajor schoolMajor) {
	    schoolMajor.setPage(page);
        page.setList(schoolMajorDao.findList(schoolMajor));
        return page;
	}

	/**
	 * 根据学校查询关联的专业
	 * @param schoolCode
	 * @return
	 */
	public List<SchoolMajor> queryBySchoolCode(String schoolCode) {
		SchoolMajor schoolMajor = new SchoolMajor();
		schoolMajor.setSchoolCode(schoolCode);
		List<SchoolMajor> schoolMajorList = schoolMajorDao.findList(schoolMajor);
		return schoolMajorList;
	}
	
	@Transactional(readOnly = false)
	public void save(School school) {
		//删除学校原有的数据
		schoolMajorDao.deleteBySchool(school.getSchoolCode());
		List<SchoolMajor> schoolMajors = school.getSchoolMajorList();
		if (schoolMajors != null && schoolMajors.size() > 0) {
			for (int i = 0; i < schoolMajors.size(); i++) {
				schoolMajors.get(i).setId(Identities.generateUUID());
				schoolMajors.get(i).setSchoolCode(school.getSchoolCode());
				schoolMajors.get(i).setSchoolName(school.getSchoolName());
			}
			schoolMajorDao.batchInsert(schoolMajors);
		}
	}

	@Transactional(readOnly = false)
	public void delete(SchoolMajor schoolMajor) {
		schoolMajorDao.delete(schoolMajor);
	}
	
}